package com.gg.user.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.gg.cart.service.CartService;
import com.gg.entity.User;
import com.gg.povos.FruitStarPovo;
import com.gg.star.service.StarService;
import com.gg.user.service.UserService;

import base.BaseServlet;


@WebServlet("/user/LoginServlet")
public class LoginServlet extends BaseServlet {
	@Autowired
	private   StarService  starService;
	
	@Autowired
	private  UserService   userService;
	@Autowired
	private  CartService   cartService;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String  loginstr = request.getParameter("loginstr");
		String  password =   request.getParameter("password");
		User  user =  new User();
		if(loginstr.contains("@")) {
			user.setEmail(loginstr);
		}else {
			user.setTel(loginstr);
		}
		user.setPassword(password);
		////   service
		user =  userService.login(user);
		if(user==null){
			request.getSession().setAttribute("msg", "用户名或密码错误！");
			response.sendRedirect("/fruits/login.jsp");
		}else {
			//查询 当前用户  所有关注过  水果 
			//List<FruitStarPovo>  fspovos= starService.getStarsByUid(user.getUid());
			//如果登陆成功， 还需要  查询回来 当前 用户  购物车条目数量
			int  cartcount =cartService.getCartCountByUid(user.getUid());
			request.getSession().setAttribute("cartcount",cartcount);
			//登录成功，  要把 user  对象放 到 session中
			request.getSession().setAttribute("user", user);
			// 把我  关注过的所有商品   放入 session里
			//request.getSession().setAttribute("fspovos", fspovos);
			
			response.sendRedirect("/fruits/index.jsp");
			
		}
		
		
	}

}
